package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.login.UserLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class AlbumInfoApiController {

	@Autowired
	private AlbumInfoService albumInfoService;

	@UserLogin
	@Operation(summary = "新增专辑")
	@PostMapping("/albumInfo/saveAlbumInfo")
	public Result saveAlbumInfo(@Validated @RequestBody AlbumInfoVo albumInfoVo){
		albumInfoService.saveAlbumInfoVo(albumInfoVo);
		return Result.ok();
	}

	@UserLogin
	@Operation(summary = "查询当前用户专辑分页列表")
	@PostMapping("/albumInfo/findUserAlbumPage/{page}/{limit}")
	public Result<Page<AlbumListVo>> findUserAlbumPage(@PathVariable("page") Long page,
													   @PathVariable("limit") Long limit,
													   @RequestBody AlbumInfoQuery albumInfoQuery){
		//获取当前用户id
		Long userId = AuthContextHolder.getUserId();
		albumInfoQuery.setUserId(userId);
		Page<AlbumListVo> pageParam = new Page<>(page,limit);
		Page<AlbumListVo> result = albumInfoService.findUserAlbumPage(pageParam,albumInfoQuery);

		return Result.ok(result);

	}

	@Operation(summary = "根据ID删除专辑")
	@DeleteMapping("/albumInfo/removeAlbumInfo/{id}")
	public Result removeAlbumInfoById(@PathVariable Long id){
		albumInfoService.removeAlbumInfoById(id);

		return Result.ok();
	}

	@Operation(summary = "修改专辑")
	@PutMapping("/albumInfo/updateAlbumInfo/{id}")
	public Result updateAlbumInfo(@PathVariable("id") Long id,
								  @Validated @RequestBody AlbumInfoVo albumInfoVo){
		albumInfoService.updateAlbumInfo(id,albumInfoVo);
		return Result.ok();
	}

	@UserLogin
	@Operation(summary = "获取当前用户全部专辑列表")
	@GetMapping("/albumInfo/findUserAllAlbumList")
	public Result<List<AlbumInfo>> findUserAllAlbumList(){
		//获取当前用户id
		Long userId = AuthContextHolder.getUserId();

		List<AlbumInfo> result = albumInfoService.findUserAllAlbumList(userId);

		return Result.ok(result);
	}

	@Operation(summary = "根据专辑ID查询专辑信息（包括专辑标签列表）")
	@GetMapping("/albumInfo/getAlbumInfo/{id}")
	public Result<AlbumInfo> getAlbumInfoById(@PathVariable Long id){
		AlbumInfo albumInfo = albumInfoService.getAlbumInfoByIdFromDB(id);
		return Result.ok(albumInfo);
	}

	@Operation(summary = "根据专辑id获取专辑统计信息")
	@GetMapping("/albumInfo/getAlbumStatVo/{albumId}")
	public Result<AlbumStatVo> getAlbumStatVo(@PathVariable Long albumId){
		AlbumStatVo albumStatVo = albumInfoService.getAlbumStatVo(albumId);
		return Result.ok(albumStatVo);
	}


	/**
	 * 基于当前选择购买起始声音得到分集购买列表
	 *
	 * @param trackId
	 * @return [{name:"本集",price:1,trackCount:1},{name:"后10集",price:10,trackCount:10}...{name:"全集/45集",price:45,trackCount:45}]
	 */
	@Operation(summary = "获取用户声音分集购买支付列表")
	@GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
	public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable("trackId") Long trackId){
		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		List<Map<String, Object>> list = albumInfoService.findUserTrackPaidList(userId,trackId);
		return Result.ok(list);
	}


}

